//
//  StockedEditVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/16.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "StockedEditVC.h"
#import "MBTUiColor.h"
#import "EditCell.h"
#import "StockDetialVC.h"
#import "DbManage.h"
@interface StockedEditVC ()<UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate>{
    
}
@property (weak, nonatomic) IBOutlet UIView *staus;
@property (weak, nonatomic) IBOutlet UIView *nav;
@property (weak, nonatomic) IBOutlet UIView *left;
@property (weak, nonatomic) IBOutlet UIView *right;
@property (weak, nonatomic) IBOutlet UILabel *titleText;
@property (weak, nonatomic) IBOutlet UIView *name;
@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation StockedEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViewBG];
    [self complete];
    [self editTable];
}


-(void)editTable{
    _table.delegate =self;
    _table.dataSource=self;
    [_table setEditing:!_table.editing animated:YES];
    [_table setAllowsSelectionDuringEditing:YES];
}

-(void)setViewBG{
     _titleText.text=@"添加自选股";
    self.navigationController.navigationBarHidden=YES;
    _staus.backgroundColor =[UIColor colorwithHexString:@"#1186D6"];
    _nav.backgroundColor =[UIColor colorwithHexString:@"#1186D6"];
    _left.backgroundColor =[UIColor colorwithHexString:@"#1186D6"];
    _right.backgroundColor =[UIColor colorwithHexString:@"#1186D6"];
    _name.backgroundColor =[UIColor colorwithHexString:@"#1186D6"];
}


-(void)complete{
    _left.userInteractionEnabled = YES;
    UITapGestureRecognizer *addTapGest = [[UITapGestureRecognizer alloc] init];
    addTapGest.numberOfTapsRequired = 1;
    addTapGest.delegate = self;
    [_left addGestureRecognizer:addTapGest];
    [addTapGest addTarget:self action:@selector(addTapView:)];
}
- (void)addTapView:(UITapGestureRecognizer *)addTapGest{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *reuseIdentifier = @"editcell";
     EditCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EditCell" owner:nil options:nil] lastObject];
    }
    cell.codeInfo= [_data objectAtIndex:indexPath.row];
    return cell;
}

//哪几行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//哪几行可以移动(可移动的行数小于等于可编辑的行数)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//移动cell时的操作
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSLog(@"排序");
    if (sourceIndexPath != destinationIndexPath) {
        NSLog(@"%ld,%ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);
        StockCode *object = [_data objectAtIndex:sourceIndexPath.row];
        [_data removeObjectAtIndex:sourceIndexPath.row];
        if (destinationIndexPath.row > [_data count]) {
            [_data addObject:object];
        }else {
            [_data insertObject:object atIndex:destinationIndexPath.row];//根据名字取update
        }
        [[DbManage shareDbManage]deletAllStock];//删除一次
        [[DbManage shareDbManage]insertStockByArray:_data];//重新插入这是排序的方法
    }
}

//当 tableview 为 editing 时,左侧按钮的 style
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"tableView");
    StockDetialVC *detal =[[StockDetialVC alloc]init];
    detal.codeInfo = [_data objectAtIndex:indexPath.row];//提取数据
    [self.navigationController pushViewController:detal animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//在设置完成的时候在这里写一个上传服务器的方法
@end
